﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyBank
{
    public partial class MainForm : Form
    {
        CurrentAccount currentAccount;
        public MainForm(CurrentAccount currentAccount)
        {
            this.currentAccount = currentAccount;
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            double temp_balance = currentAccount.account_Tempbalance;
            if (CurrentAccount.isAdmin == 0) toolStripStatusLabel3.Text = "用户";
            else toolStripStatusLabel3.Text = "管理员";
            int hour = DateTime.Now.Hour;
            string wel;
            load_Combo();

            combo_Cards.SelectedIndex = 0;
            combo_Cards2.SelectedIndex = 0;
            combo_transBanks.SelectedIndex = 0;
            combo_banks.SelectedIndex = 0;

            if (hour < 8) wel = "早上好！";
            else if (hour < 11) wel = "上午好！";
            else if (hour < 14) wel = "中午好！";
            else if (hour < 18) wel = "下午好！";
            else wel = "晚上好！";

            label_time.Text = DateTime.Now.ToLongDateString().ToString();
            label_welcome.Text = wel + CurrentAccount.account_Name + "!";
            label4.Text = currentAccount.account_Tempbalance.ToString();
        }


        private void load_Combo()
        {
            for (int i = 0;i < CurrentAccount.all_Cards.Count; i++)
            {
                combo_Cards.Items.Add(CurrentAccount.all_Cards[i].Card_Name);
                combo_Cards2.Items.Add(CurrentAccount.all_Cards[i].Card_Name);
                combo_transBanks.Items.Add(CurrentAccount.all_Cards[i].Card_Name);
                
            }
            combo_Cards2.Items.Add("临时钱包");


            for (int i = 0; i < currentAccount.Banks.Count; i++)
            {
                combo_banks.Items.Add(currentAccount.Banks[i].Bank_Name);
            }
            
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }


        private void btn_query_Click(object sender, EventArgs e)
        {
            currentAccount.load_Cards();
            label_balance.Text = CurrentAccount.all_Cards[combo_Cards.SelectedIndex].Card_Balance.ToString();
            btm_alert.Text = "银行卡余额查询成功！";
        }

        private void combo_BankCards_SelectedIndexChanged(object sender, EventArgs e)
        {
            label_CardText.Text = currentAccount.Banks[combo_banks.SelectedIndex].bank_cards[combo_BankCards.SelectedIndex].Card_Text;
        }

        private void combo_banks_SelectedIndexChanged(object sender, EventArgs e)
        {
            combo_BankCards.Items.Clear();
            for (int i = 0; i < currentAccount.Banks[combo_banks.SelectedIndex].bank_cards.Count; i++)
            {
                combo_BankCards.Items.Add(currentAccount.Banks[combo_banks.SelectedIndex].bank_cards[i].Card_Name);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            double preBalance = int.Parse(text_preBalance.Text);
            int Bank_ID = currentAccount.Banks[combo_banks.SelectedIndex].Bank_ID;
            int Card_ID = currentAccount.Banks[combo_banks.SelectedIndex].bank_cards[combo_BankCards.SelectedIndex].Card_ID;
            string Card_Name = currentAccount.Banks[combo_banks.SelectedIndex].bank_cards[combo_BankCards.SelectedIndex].Card_Name;
            currentAccount.add_Card(preBalance, Card_ID, Bank_ID,Card_Name);
            MessageBox.Show("办理成功！！！");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (int.Parse(textBox1.Text) % 100 != 0)
            {
                MessageBox.Show("必须为100的整数倍！！");
                return;
            }else if (combo_Cards2.SelectedIndex == combo_Cards2.Items.Count - 1)
                {
                    if (currentAccount.SaveMoney(double.Parse(textBox1.Text)))
                    {
                        CurrentAccount.sql.Log(DateTime.Now.ToString(), CurrentAccount.account_ID, "存入", double.Parse(textBox1.Text), 0);
                        MessageBox.Show("临时钱包存款成功！");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("临时钱包失败！");
                        return;
                    }
                }
            
            if (CurrentAccount.all_Cards[combo_Cards2.SelectedIndex].SaveMoney(double.Parse(textBox1.Text)))
            {

                MessageBox.Show("存款成功！");
            }
            else MessageBox.Show("存款失败！");
        }

        private void btn_transfer_Click(object sender, EventArgs e)
        {
            if (!currentAccount.checkID(text_transAccount.Text))
            {
                return;
            }
            if (CurrentAccount.all_Cards[combo_transBanks.SelectedIndex].WithDrawMoney(double.Parse(text_transMoney.Text)))
            {
                // 存入临时钱包
                if (double.Parse(text_transMoney.Text) < 0)  // 卫语句（存入金额不能为负）
                    MessageBox.Show("转账失败！！");
                 SQLiteDataReader reader = CurrentAccount.sql.ReadFullTable("Account");
                 while (reader.Read())
                {
                    if (int.Parse(text_transAccount.Text) == reader.GetDouble(reader.GetOrdinal("account_ID")))
                    {
                        CurrentAccount.sql.Log(DateTime.Now.ToString(), int.Parse(text_transAccount.Text), CurrentAccount.account_ID.ToString()+"转账", double.Parse(text_transMoney.Text), 0);
                        double oldMoney = reader.GetDouble(reader.GetOrdinal("account_Tempbalance"));
                        CurrentAccount.sql.UpdateValues("Account", new string[] { "account_Tempbalance" }, new string[] { (oldMoney + double.Parse(text_transMoney.Text)).ToString() }, "account_ID", text_transAccount.Text);
                        MessageBox.Show("转账成功！！");
                    }
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (int.Parse(textBox1.Text) % 100 != 0)
            {
                MessageBox.Show("必须为100的整数倍！！");
                return;
            }
            else
            {
                if(combo_Cards2.SelectedIndex == combo_Cards2.Items.Count - 1)
                {
                    if (currentAccount.WithDrawMoney(double.Parse(textBox1.Text)))
                    {
                        MessageBox.Show("临时钱包取款成功！");
                        return;
                    }
                    else { 
                        MessageBox.Show("临时钱包取款失败！");
                        return;
                    }
                }
                if (CurrentAccount.all_Cards[combo_Cards2.SelectedIndex].WithDrawMoney(double.Parse(textBox1.Text)))
                {
                    MessageBox.Show("取款成功！");
                }else MessageBox.Show("取款失败！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            currentAccount.load_temp();
            label4.Text = currentAccount.account_Tempbalance.ToString();
        }



        // 查询记录
        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            SQLiteDataReader reader = CurrentAccount.sql.ReadFullTable("records");
            int temp_id;
            while (reader.Read())
            {
                temp_id = reader.GetInt32(reader.GetOrdinal("Account_ID"));
                if (temp_id == CurrentAccount.account_ID)
                {
                    listBox1.Items.Add(reader.GetString(reader.GetOrdinal("time")) + "---" + reader.GetString(reader.GetOrdinal("type")) + "---" + reader.GetInt32(reader.GetOrdinal("num")) + "---" + reader.GetInt32(reader.GetOrdinal("Card_ID")).ToString());
                }
            }
            reader.Close();
        }
    }
}
